package com.github.microwww.upms.controller;

import com.github.microwww.upms.service.UserRoleService;
import com.github.microwww.upms.vo.UserRoleValue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.List;
import java.util.stream.Collectors;

@RestController()
@RequestMapping(value = "/user-role")
public class UserRoleController {

    @Autowired()
    UserRoleService userRoleService;

    @GetMapping(value = "/detail")
    public UserRoleValue.Info detailUserRole(int id) {
        return this.userRoleService.findById(id).map(UserRoleValue.Info::new).orElseGet(null);
    }

    @GetMapping(value = "/list")
    public List<UserRoleValue.Simple> listUserRole(@RequestParam(defaultValue = "0") int page, @RequestParam(defaultValue = "10") int size) {
        return this.userRoleService.findAll(page, size).stream().map(UserRoleValue.Simple::new).collect(Collectors.toList());
    }
}
